package com.clstu.jdbctest;

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 这是一个用于测试mysql连接的用例
 */
public class ResultSet_ {
    public static void main(String[] args) throws SQLException {
        //1.注册驱动
        Driver driver = new Driver();

        //2.得到连接
        String url = "jdbc:mysql://localhost:3306/cl_db01";
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","root");
        Connection connect = driver.connect(url, properties);//得到网络连接,类似数据通路

        //3.执行sql
        String id= "'至尊宝'";
        String sql = "select id, pwd from user where id = "+id;
        Statement statement = connect.createStatement();//用于执行sql语句并返回生成的结果对象

        ResultSet resultSet = statement.executeQuery(sql);//返回单个的结果集和

        //使用while循环来从resultSet中拿到数据
//        System.out.println(resultSet.next());//没有对象返回就是一个false
        if(resultSet.next()){
            System.out.println(resultSet.getString(1)+"\t"+resultSet.getString(2)+"\t");
        }

        //4.关闭连接,释放资源
        statement.close();
        connect.close();
    }
}
